package com.jsm.tf.service;

import com.jsm.tf.entity.User;
import com.jsm.tf.exception.BusinessException;
import com.jsm.tf.mapper.UserMapper;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import java.util.List;

@Service
public class UserService {
    @Resource
    private UserMapper userMapper;

    public void register(String username,String password,String nickname)  {
        Example example = Example.builder(User.class).build();
        example.createCriteria().andEqualTo("username",username);
        User db = userMapper.selectOneByExample(example);
        if(db!=null){
            throw new BusinessException("该用户已存在");
        }
        User user=new User();
        user.setUsername(username);
        user.setPassword(password);
        user.setNickname(nickname);
        userMapper.insert(user);
    }

    public User getByUserNameAndPassword(String username, String password) {
        Example example = Example.builder(User.class).build();
        example.createCriteria().andEqualTo("username",username)
                .andEqualTo("password",password);
        return userMapper.selectOneByExample(example);
    }

    public User login(String username,String password) {
        User user = this.getByUserNameAndPassword(username, password);
        return user;
    }

    public List<User> listByIds(List<Long> ids) {
        return userMapper.selectByIdList(ids);
    }
}
